
var apiHost = wx.getStorageSync('apiHost');
var apiUpLoad = wx.getStorageSync('apiUpLoad');        //上传外网图片地址

Page({

  /**
   * 页面的初始数据
   */
  data: {
    winWidth : '',
    winHeight : '',
    headImage:'',//头像路径
    QRImage:'',
    userName:'',
    aboutMe:'',
    userId:'',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var userId = wx.getStorageSync('userId');

    wx.getSystemInfo({
      success: function(res) { 
         var winWidth = res.windowWidth;
         var winHeight = res.windowHeight;
         that.setData({
           winWidth:winWidth,
           winHeight:winHeight,
           userId : userId,
         })
      },
    })
    that.getData(that);
  },
  /**
   * 获取用户数据
   */
  getData:function(that){

    wx.request({
      url: apiHost + '/hpp/user/getinfo',
      data: {
        userId:that.data.userId,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data)
        var userInfo = res.data.data;
        that.setData({
          headImage: userInfo.headImg,
          userName : userInfo.nickName,
          QRImage: userInfo.qrCode,
          aboutMe :userInfo.aboutMe,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 点击提交
   */
  formSubmit:function(e){
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    var that = this;
    var value = e.detail.value;
    if(value.userName == ''){
      wx.showToast({
        title: '请填写姓名',
        icon:'none'
      })
      return;
    }
    wx.showToast({
      icon: "loading",
      title: "请求中"
    }),
    wx.request({
      url: apiHost + '/hpp/user/modify',
      data:{
        userId:userId,
        headImage:that.data.headImage,
        userName:value.userName,
        qrCode:that.data.QRImage,
        aboutMe:value.aboutMe,
      },
      success:function(res){
        wx.hideLoading();
        console.log('提交修改', res.data);
        if(res.data.code == '10000'){
          wx.navigateBack({
            
          });
        }else{
          wx.showToast({
            title: res.data.msg,
            icon:'none',
          })
        }
      }
    })
  },
  /**
   * 选择头像
   */
  choosHeadImage:function(){
    var that = this;
    wx.chooseImage({
      success: function(res) {
        console.log('选择相册',res)
        // that.setData({
        //   headImage:res.tempFilePaths[0],
        // })
        that.uploadImage(0, res.tempFilePaths[0]);//上传头像
      },
      count:1,
    })
  },
  /**
   * 选择二维码
   */
  choosQrCode:function(){
    var that = this;
    wx.chooseImage({
      success: function (res) {
        console.log('选择相册', res)
        // that.setData({
        //   QRImage: res.tempFilePaths[0],
        // })
        that.uploadImage(1, res.tempFilePaths[0]);//上传二维码
      },
      count: 1,
    })
    
    
  },
/**
 * 上传图片到服务器
*/
uploadImage:function(type,path){//type:0 头像 1 二维码
  var that = this;
  wx.showToast({
    icon: "loading",
    title: "正在上传"
  }),
  wx.uploadFile({
    url: apiUpLoad + "image-server/uploadImage/oss", 
    filePath: path,
    name: 'file',
    header: { "Content-Type": "multipart/form-data" },
    formData: {
      //和服务器约定的token, 一般也可以放在header中
      'session_token': wx.getStorageSync('session_token')
    },
    success: function (res) {
      var data = res.data;
      var jsonData = JSON.parse(data);
      console.log('图片回调', jsonData)
      if(type == 0){
        that.setData({
          headImage:jsonData.data.url,
        })
      }else{
        that.setData({
          QRImage: jsonData.data.url,
        })
      }

    },
    fail: function (e) {
      wx.showToast({
        title: e.errMsg,
        icon: 'none',
        duration: 2000,
        mask: true
      });
    },
    complete: function () {
      wx.hideToast();  //隐藏Toast
    }
  })
},

})